// pages/toexam/toexam.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentIndex: 0,// 当前的试题
    correctOptionId: null, // 正确选项id
    knowledgeId: null, //知识id
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    this.setData({
      knowledgeId: options.knowledgeId
    })
    this.getAllQuestionByKnowledgeId(options.knowledgeId)
  },

  /**
   * 通过知识id获取知识下的所有题目
   * exam/getAllQuestionByKnowledgeId
   */
  getAllQuestionByKnowledgeId(knowledgeId) {
    let that = this;
    app.util.request({
      url: "exam/getAllQuestionByKnowledgeId/" + knowledgeId,
      method: "get",
      success: function (res) {

        if (res.data.code == 200) {
          that.setData({
            questionList: res.data.data
          })
        }
      },
      fail: function () {
        wx.showToast({
          title: '网路异常',
        })
      }
    })
  },

  /**
   * 上一题
   */
  lastQuestion() {
    if (this.data.currentIndex == 0) {
      wx.showToast({
        title: '这是第一题',
        icon: 'none'
      })
      return;
    }
    let currentIndex = this.data.currentIndex - 1
    this.setData({
      currentIndex: currentIndex
    })
  },

  /**
   * 下一题
   */
  nextQuestion() {

    let that = this;
    let questionList = this.data.questionList;
    let currentIndex = this.data.currentIndex

    let correctOptionId = null
    questionList[currentIndex].appletQuestionOptionList.forEach(item => {
      if (item.checked) {
        correctOptionId = item.id
      }
    })
    if (correctOptionId == null) {
      wx.showToast({
        title: '请选择答案',
        icon: 'none'
      })
      return;
    }

    let queryParam = "?questionId=" + questionList[currentIndex].id;
    queryParam += "&optionId=" + correctOptionId;
    queryParam += "&userId=" + app.globalData.userInfo.id
    app.util.request({
      url: "exam/putQuestionAnswer" + queryParam,
      method: "post",
      showLoading: true,
      success: function (res) {

        if (res.data.code == 200) {
          let currentIndex = that.data.currentIndex;

          if (currentIndex >= that.data.questionList.length - 1) {
            wx.showToast({
              title: '已经是最后一道题了',
              icon: 'none'
            })
            return;
          }
          currentIndex = currentIndex + 1
          that.setData({
            currentIndex: currentIndex,
          })
        }
      },
      fail: function () {
        wx.showToast({
          title: '网路异常',
        })
      }
    })


  },

  /**
   * 提交各个试题
   */
  submitOne() {



    let questionList = this.data.questionList;
    let currentIndex = this.data.currentIndex

    let correctOptionId = null
    questionList[currentIndex].appletQuestionOptionList.forEach(item => {
      if (item.checked) {
        correctOptionId = item.id
      }
    })
    if (correctOptionId == null) {
      wx.showToast({
        title: '请选择答案',
        icon: 'none'
      })
      return;
    }

    let queryParam = "?questionId=" + questionList[currentIndex].id;
    queryParam += "&optionId=" + correctOptionId;
    queryParam += "&userId=" + app.globalData.userInfo.id
    app.util.request({
      url: "exam/putQuestionAnswer" + queryParam,
      method: "post",
      success: function (res) {

        if (res.data.code == 200) {

        }
      },
      fail: function () {
        wx.showToast({
          title: '网路异常',
        })
      }
    })
  },

  /**
   * 交卷
   */
  finishExam() {
    let that = this;
    let questionList = this.data.questionList
    let currentIndex = this.data.currentIndex
    if (currentIndex == questionList.length - 1) {
      let correctOptionId = null
      questionList[currentIndex].appletQuestionOptionList.forEach(item => {
        if (item.checked) {
          correctOptionId = item.id
        }
      })
      if (correctOptionId == null) {
        wx.showToast({
          title: '请选择答案',
          icon: 'none'
        })
        return;
      }

      let queryParam = "?questionId=" + questionList[currentIndex].id;
      queryParam += "&optionId=" + correctOptionId;
      queryParam += "&userId=" + app.globalData.userInfo.id
      app.util.request({
        url: "exam/putQuestionAnswer" + queryParam,
        method: "post",
        showLoading: true,
        success: function (res) {

          if (res.data.code == 200) {
            queryParam = "";
            queryParam = "?knowledgeId=" + that.data.knowledgeId;
            queryParam += "&userId=" + app.globalData.userInfo.id

            app.util.request({
              url: "exam/finishExam" + queryParam,
              method: "post",
              success: function (res) {

                if (res.data.code == 200) {
                  wx.showModal({
                    title: '提示',
                    content: '交卷成功,即将跳转到考试主页！',
                    showCancel: false,
                    success(res) {
                      if (res.confirm) {
                        wx.switchTab({
                          url: '/pages/exam/exam',
                        })
                      }
                    }
                  })
                } else {
                  wx.showToast({
                    title: '系统异常',
                    icon: 'none'
                  })
                }
              },
              fail: function () {
                wx.showToast({
                  title: '网路异常',
                })
              }
            })


          }
        },
        fail: function () {
          wx.showToast({
            title: '网路异常',
          })
        }
      })


      return;
    }

    let queryParam = "?knowledgeId=" + this.data.knowledgeId;
    queryParam += "&userId=" + app.globalData.userInfo.id

    app.util.request({
      url: "exam/finishExam" + queryParam,
      method: "post",
      success: function (res) {

        if (res.data.code == 200) {
          wx.showModal({
            title: '提示',
            content: '交卷成功,即将跳转到考试主页！',
            showCancel: false,
            success(res) {
              if (res.confirm) {
                wx.switchTab({
                  url: '/pages/exam/exam',
                })
              }
            }
          })
        } else {
          wx.showToast({
            title: '系统异常',
            icon: 'none'
          })
        }
      },
      fail: function () {
        wx.showToast({
          title: '网路异常',
        })
      }
    })
  },


  radioChange(e) {

    let questionList = this.data.questionList;
    let currentIndex = this.data.currentIndex


    questionList[currentIndex].appletQuestionOptionList.forEach(item => {
      item.checked = item.id == e.detail.value ? true : false
    })



    console.log('radio发生change事件，携带value值为：', e.detail.value)

    this.setData({
      correctOptionId: e.detail.value,
      questionList: questionList
    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})